package cst.neu.yuejian_31.model;

import cst.neu.yuejian_31.bean.ActivityCollectBean;
import cst.neu.yuejian_31.iface.ActivityCollectIface;
import cst.neu.yuejian_31.iface.ActivityCollectListener;
import cst.neu.yuejian_31.service.ActivityCollectService;
import cst.neu.yuejian_31.Common.Constant;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Alienware on 2017/10/29.
 */

public class ActivityCollectMdoel implements ActivityCollectIface {
    private Retrofit retrofit;
    public ActivityCollectMdoel(){
        retrofit=new Retrofit.Builder()
                .baseUrl(Constant.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    @Override
    public void activityCollect(String userId, String activityId, final ActivityCollectListener activityCollectListener) {
        ActivityCollectService activityCollectService=retrofit.create(ActivityCollectService.class);
        Call<ActivityCollectBean> call=
                activityCollectService.activityCollect(userId,activityId);
        call.enqueue(new Callback<ActivityCollectBean>() {
            @Override
            public void onResponse(Call<ActivityCollectBean> call, Response<ActivityCollectBean> response) {
                if(response.isSuccessful()&&response.body()!=null){
                    activityCollectListener.onSuccess(response.body());
                }else {
                    activityCollectListener.onFail("Fail");
                }
            }

            @Override
            public void onFailure(Call<ActivityCollectBean> call, Throwable t) {
                activityCollectListener.onFail(t.toString());
            }
        });
    }
}
